angular.module('ionicApp.contact', [])

.controller('contactCtrl', function($scope, $ionicModal, userService, $ionicPopup) {
    // 创建并载入模型
    $ionicModal.fromTemplateUrl('templates/contact/add_user.html', {
        scope: $scope,
        animation: 'slide-left-right'
    }).then(function(modal) {
        $scope.newOneModal = modal;
    });

    $ionicModal.fromTemplateUrl('templates/contact/search_res.html', {
        scope: $scope,
        animation: 'slide-left-right'
    }).then(function(modal) {
        $scope.showSearchResult = modal;
    });

    $ionicModal.fromTemplateUrl('templates/contact/show_user.html', {
        scope: $scope,
        animation: 'slide-left-right'
    }).then(function(modal) {
        $scope.showDetail = modal;
    });

    $scope.method = {
        init: function() {
            $scope.userList = userService.getUsers();
            $scope.search = {
                text: "",
                res: []
            };
            $scope.newModel = {
                op: "",
                userid: "",
                cate: "",
                gender: "",
                name: "",
                phone: "",
                cell: "",
                address: "",
                email: "",
                avatar: ""
            };
            $scope.detail = {
                userid: "",
                cate: "",
                gender: "",
                name: "",
                phone: "",
                cell: "",
                address: "",
                email: "",
                avatar: ""
            };
            $scope.fileread = {
                Image: {
                    url: "",
                    name: ""
                }
            }
        },
        modalCtrl: function(ctn, op, callback) {
            var modalCtn = {
                "newOne": $scope.newOneModal,
                "searchRes": $scope.showSearchResult,
                "showDetail": $scope.showDetail,
            };
            // 传入多个modal，执行统一操作, op === 'hide' || op === 'show'
            if (angular.isArray(ctn)) {
                op === "hide" ? ctn.forEach(function(item, index) {
                    modalCtn[item].hide();
                }) : ctn.forEach(function(item, index) {
                    modalCtn[item].show();
                });
                return false;
            }

            op === "hide" ? modalCtn[ctn].hide() : modalCtn[ctn].show();
            callback && callback.call(null);
        },
        addUser: function(user) {
            var that = this;
            if (user.op === "edit") {
                userService.editUser($scope.detail, user);
            } else {
                userService.addUser(angular.copy(user));
            }

            that.modalCtrl("newOne", "hide");
            that.init();
        },
        delUser: function(user) {
            var that = this;
            $ionicPopup.confirm({
                title: "确认删除此条记录吗？</br>此操作不可逆！"
            }).then(function(res) {
                if (res) {
                    userService.delUser(user);
                }
                that.init();
            });
        },
        editUser: function(user) {
            this.modalCtrl('showDetail', 'hide');
            this.modalCtrl('newOne', 'show');

            $scope.newModel = angular.copy(user);
            $scope.newModel.op = 'edit';
        },
        showUser: function(user) {
            $scope.method.modalCtrl("showDetail", "show");
            $scope.detail = {
                userid: user.userid,
                name: user.name,
                gender: user.gender,
                avatar: user.avatar,
                phone: user.phone,
                cell: user.cell,
                email: user.email,
                address: user.address,
                cate: user.cate
            }
        },
        fileMenu: function() {
            document.getElementById('file_select').click();
            // -----------------Cordova fileUpload plugin
            // var type = 'gallery';
            // $ionicActionSheet.show({
            //     buttons: [
            //         { text: '拍照' },
            //         { text: '从相册选择' }
            //     ],
            //     titleText: '选择照片',
            //     cancelText: '取消',
            //     cancel: function() {},
            //     buttonClicked: function(index) {
            //         if (index == 0) {
            //             type = 'camera';
            //         } else if (index == 1) {
            //             type = 'gallery';
            //         }　　　　　　　 
            //         //Camera.getPicture(type)->根据选择的“选取图片”的方式进行选取
            //         Camera.getPicture(type).then(　　　//返回一个imageURI，记录了照片的路径
            //             function(imageURI) {
            //                 $scope.me.image = imageURI;　　　　//更新页面上的照片
            //                 $scope.img = imageURI;
            //                 $scope.$apply();
            //             },
            //             function(err) {});
            //         return true;
            //     }
            // });
        }
    }

    $scope.method.init();

    // 自定义监视, 通过对象包装
    $scope.$watch("search.text", function(newValue, oldValue) {
        if (newValue === oldValue) {
            return true;
        }
        $scope.search.res = userService.searchUser(newValue);
    }, true);

    // 图片预览，因异步执行，所以通过监控实现
    $scope.$watch("fileread.Image.url", function(newValue, oldValue) {
        if (newValue === oldValue || newValue == "") {
            return true;
        }
        $scope.newModel.avatar = newValue;
    }, true)
});
